//
//  ViewController.m
//  ShopCarDelete
//
//  Created by xhl on 16/7/7.
//  Copyright © 2016年 xhl. All rights reserved.
//

#import "ViewController.h"
#import "ShopCarGroupModel.h"
#import "ShopCarCell.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic,strong) NSMutableArray *cellData;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
}

/**
 *  懒加载数据  这里就类似于从网络上请求下来的数据，然后做处理
 *
 *  @return 数据源
 */
- (NSMutableArray *)cellData
{
    if (!_cellData) {
        _cellData = [NSMutableArray array];
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"ShopCarData.plist" ofType:nil];
        NSArray *datas = [NSArray arrayWithContentsOfFile:path];
        
        for (NSDictionary *dic in datas) {
            ShopCarGroupModel *model = [ShopCarGroupModel shopCarGroupModelWithDic:dic];
            [_cellData addObject:model];
        }
        
    }
    return _cellData;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *headerLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 20)];
    headerLbl.textColor = [UIColor grayColor];
    headerLbl.backgroundColor = [UIColor redColor];
    headerLbl.textAlignment = NSTextAlignmentCenter;
    if (self.cellData.count > section) {
        ShopCarGroupModel *model = self.cellData[section];
        headerLbl.text = model.headerTitle;
    }
    
    return headerLbl;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.cellData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //防止数组越界
    if (self.cellData.count > section) {
        ShopCarGroupModel *model = self.cellData[section];
        return model.sectionDatas.count;
    }else{
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShopCarCell *cell = [ShopCarCell backCell:tableView];
    //防止数组越界
    if (self.cellData.count > indexPath.section) {
        ShopCarGroupModel *model = self.cellData[indexPath.section];
        //防止数组越界
        if (model.sectionDatas.count >indexPath.row) {
            [cell cellDataWithModel:model.sectionDatas[indexPath.row] andIndexPath:indexPath];
        }
    }
    
    __weak typeof(self) weakSelf = self;
    cell.callBack = ^(NSInteger tag){
        
        //防止数组越界
        ShopCarGroupModel *model = weakSelf.cellData[indexPath.section];
        [model.sectionDatas removeObjectAtIndex:tag];
        //如果section对应数组为0  删除对应数组 然后刷新整个界面
        if (model.sectionDatas.count == 0) {
            [weakSelf.cellData removeObjectAtIndex:indexPath.section];
            [weakSelf.tableView reloadData];
            return;
        }else{
            //只刷新当前组的数据
            NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.section];
            [weakSelf.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
        }
        
       
    };
    
    return cell;
}

@end
